home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / gsdbloo.exe / GS_EDIT.PAS < prev    next >
Pascal/Delphi Source File  |  1992-02-24  |  23KB  |  772 lines

  1. Unit GS_Edit;
  2. {-----------------------------------------------------------------------------
  3.                                  Editor Routines
  4.  
  5.        GS_Edit Copyright (c)  Richard F. Griffin
  6.  
  7.        10 January 1991
  8.  
  9.        102 Molded Stone Pl
  10.        Warner Robins, GA  31088
  11.  
  12.        -------------------------------------------------------------
  13.        This unit handles the objects for a simple editor similar to
  14.        WordStar.
  15.  
  16.        Changes:
  17.  
  18.        18 May 91 - Modified Presssed_Del to bring up the succeeding line
  19.                    if the cursor is at the end of the line.
  20.  
  21.        06 Jun 91 - Modified startup routine to initialize the windows.  all
  22.                    instances will use the same window object.
  23.  
  24.        20 Feb 92 - Added a Done destructor to allow dynamic allocation and
  25.                    deallocation of the object.
  26.  
  27. ------------------------------------------------------------------------------}
  28.  
  29. interface
  30. {$D-}
  31.  
  32. uses
  33.    CRT,
  34.    Dos,
  35.    GS_KeyI,
  36.    GS_Scrn,
  37.    GS_Winfc,
  38.    GS_Error,
  39.    GS_Strng;
  40. type
  41.    GS_Edit_Pntr = ^GS_Edit_Line;
  42.    GS_Edit_Line = record
  43.                      Next_Line,
  44.                      Prev_Line  : GS_Edit_Pntr;
  45.                      Return_Cod : byte;
  46.                      Line_Size  : integer;
  47.                      Valu_Line  : string;
  48.                   end;
  49.  
  50.    GS_Edit_Blok = record
  51.                      Blok_Line,
  52.                      Blok_Colm  : integer;
  53.                   end;
  54.  
  55.    GS_Edit_Objt = object
  56.                      First_Line,
  57.                      End_Line,
  58.                      Work_Line   : GS_Edit_Pntr;
  59.                                       {Used to track lines}
  60.                      Cursor_LocX,
  61.                      Cursor_LocY : word;
  62.                                       {Hold cursor location}
  63.                      Active_Line,     {Current line number}
  64.                      Total_Lines,     {Total number of lines}
  65.                      Screen_Top,      {Line number at top of screen}
  66.                      Screen_Btm  : longint;
  67.                                       {Line Number at bottom of screen}
  68.                      CursorPos   : integer;
  69.                                       {Position in line}
  70.                      CursorLine  : integer;
  71.                                       {Line currently working on}
  72.                      Temp_Line   : string;
  73.                                       {work area during wordwrap}
  74.                      Edit_Lgth   : integer;
  75.                                       {Max size of eaach line}
  76.                      Lines_Avail : integer;
  77.                                       {Number of lines that will fit in the}
  78.                                       {window on the screen}
  79.                      Ch_Work     : char;
  80.                                       {Hold area for keystrokes}
  81.                      Word_Wrap   : boolean;
  82.                                       {True sets word wrap on}
  83.                      WW_Flag     : boolean;
  84.                                       {Internal flag for wordwrap condition}
  85.                      Blok_Begin,
  86.                      Blok_Fini   : GS_Edit_Blok;
  87.                                       {Future use for block operations}
  88.  
  89.  
  90.                      constructor Init;
  91.                      destructor  Done;
  92.                      function    Byte_Count : longint;
  93.                      procedure   Check_Func_Keys;
  94.                      procedure   Clear_Editor;
  95.                      procedure   Edit;
  96.                      Procedure   Edit_Line;
  97.                      function    Find_Line(linenum : integer) : boolean;
  98.                      function    Get_Line_Mem(lth : integer) : pointer;
  99.                      Procedure   Rel_Line_Mem(linenum : integer);
  100.                      Procedure   Show_Lines(b, e :integer);
  101.                      Procedure   View;
  102.                      Procedure   WordWrap(Fline : string);
  103.                      Procedure   Pressed_Bsp;
  104.                      Procedure   Pressed_CrtlY;
  105.                      Procedure   Pressed_Del;
  106.                      Procedure   Pressed_DnAr;
  107.                      Procedure   Pressed_F1;
  108.                      Procedure   Pressed_Ret;
  109.                      Procedure   Pressed_UpAr;
  110.                      Procedure   Pressed_PgUp;
  111.                      Procedure   Pressed_PgDn;
  112.                   end;
  113.  
  114.  
  115. implementation
  116. var
  117.    StatWin,
  118.    HelpWin,
  119.    EditWin  : GS_Wind_Objt;
  120.  
  121. constructor GS_Edit_Objt.Init;
  122. begin
  123.    First_Line := nil;
  124.    End_Line := nil;
  125.    Work_Line := nil;
  126.    Word_Wrap := true;
  127.    WW_Flag := false;
  128.    Active_Line := 0;
  129.    Total_Lines := 0;
  130.    Screen_Top := 0;
  131.    Screen_Btm := 0;
  132.    Ch_Work := #0;
  133.    CursorPos := 1;
  134.    CursorLine := 1;
  135.    Temp_Line := '';
  136.    GS_KeyI_Ins := True;               {Start in insert mode}
  137.    Edit_Lgth := 32;
  138. end;
  139.  
  140. destructor GS_Edit_Objt.Done;
  141. begin
  142.    Clear_Editor;
  143. end;
  144.  
  145. function GS_Edit_Objt.Byte_Count : longint;
  146. var
  147.    i : longint;
  148.    p : GS_Edit_Pntr;
  149. begin
  150.    i := 0;
  151.    p := First_Line;
  152.    while (p <> nil) do
  153.    begin
  154.       i := i + length(p^.Valu_Line) + 2;
  155.                                       {Add length of line + CR/LF chars}
  156.       p := p^.Next_Line;
  157.    end;
  158.    inc(i);                            {Add one for EOF byte}
  159.    Byte_Count := i;
  160. end;
  161.  
  162. procedure GS_Edit_Objt.Clear_Editor;
  163. begin
  164.    Work_Line := First_Line;
  165.    while (Work_Line <> nil) do
  166.    begin
  167.       End_Line := Work_Line^.Next_Line;
  168.       FreeMem(Work_Line,Work_Line^.Line_Size);
  169.       Work_Line := End_Line;
  170.    end;
  171.    First_Line := nil;
  172.    End_Line := nil;
  173.    Work_Line := nil;
  174.    Active_line := 0;
  175.    Total_Lines := 0;
  176. end;
  177.  
  178.  
  179. procedure GS_Edit_Objt.Pressed_F1;
  180. var
  181.    cc : char;
  182. begin
  183.    HelpWin.SetWin;
  184.    writeln('Toggle Ins  - Ins');
  185.    writeln('Delete Char - Del');
  186.    writeln('Delete Line - Ctl-Y');
  187.    writeln('Press any Key');
  188.    cc := ReadKey;
  189.    if cc = #0 then cc := ReadKey;
  190.    HelpWin.RelWin;
  191. end;
  192.  
  193.  
  194. procedure GS_Edit_Objt.Pressed_Bsp;
  195. var
  196.    bb : byte;
  197.    ss : string;
  198.    ll : boolean;
  199. begin
  200.    if CursorPos > 1 then
  201.    begin
  202.       Delete(Work_Line^.Valu_Line, Pred(CursorPos), 1);
  203.       GoToXY(1, CursorLine);
  204.       Write(Work_Line^.Valu_Line);
  205.       ClrEol;
  206.       Dec(CursorPos);
  207.    end
  208.    else
  209.    begin
  210.       if Active_Line > 1 then
  211.       begin
  212.          bb := Work_line^.Return_Cod;
  213.          ss := Work_Line^.Valu_Line;
  214.          if Active_Line < Total_Lines then
  215.          begin
  216.             Pressed_CrtlY;
  217.             Pressed_UpAr;
  218.          end else Pressed_CrtlY;
  219.          Work_Line^.Return_Cod := bb;
  220.          ss := Work_Line^.Valu_Line + ss;
  221.          CursorPos := length(Work_Line^.Valu_Line);
  222.          WordWrap(ss);
  223.          GotoXY(1,succ(Active_Line-Screen_Top));
  224.          write(Work_Line^.Valu_Line);
  225.       end;
  226.    end;
  227. end;
  228.  
  229. procedure GS_Edit_Objt.Pressed_Del;
  230. begin
  231.    if CursorPos <= Length(Work_Line^.Valu_Line) then
  232.    begin
  233.       Delete(Work_Line^.Valu_Line, CursorPos, 1);
  234.       GoToXY(1, CursorLine);
  235.       Write(Work_Line^.Valu_Line);
  236.       ClrEol;
  237.    end
  238.    else
  239.    begin
  240.       if Active_Line < Total_Lines then
  241.       begin
  242.          Pressed_DnAr;
  243.          CursorPos := 1;
  244.          Pressed_Bsp;
  245.       end;
  246.    end;
  247. end;
  248.  
  249. procedure GS_Edit_Objt.Pressed_PgDn;
  250. begin         {Page Down}
  251.    Active_Line := pred(Screen_Top + Lines_Avail);
  252.    if Active_Line > Total_Lines then Active_Line := Total_Lines;
  253.    if not Find_Line(Active_Line) then
  254.    begin
  255.       ShowError(710,'Pressed_PgDn');
  256.       exit;
  257.    end;
  258.    if Active_Line <> Screen_Top then Show_Lines(Active_Line,Total_Lines);
  259.    CursorLine := 1;
  260.    if length(Work_Line^.Valu_Line)+1 < CursorPos then
  261.       CursorPos := length(Work_Line^.Valu_Line)+1;
  262. end;
  263.  
  264. procedure GS_Edit_Objt.Pressed_PgUp;
  265. begin         {Page Up}
  266.    if Active_Line <= 1 then exit;
  267.    Active_Line := succ(Screen_Top - Lines_Avail);
  268.    if Active_Line < 1 then Active_Line := 1;
  269.    if not Find_Line(Active_Line) then
  270.    begin
  271.       ShowError(710,'Pressed_PgUp');
  272.       exit;
  273.    end;
  274.    if Active_Line < Screen_Top then Show_Lines(Act